﻿Imports TransportadoraToledoDAL
Public Class AgendamentoBLL
    Public Sub New()

    End Sub
    Dim objDAL As AgendamentoDAL
#Region "Propriedades"
    Private age_codigo As Integer
    Public Property pAge_codigo() As Integer
        Get
            Return age_codigo
        End Get
        Set(ByVal value As Integer)
            age_codigo = value
        End Set
    End Property
    Private cod_cli_remetente As Integer
    Public Property pCod_cli_remetente() As Integer
        Get
            Return cod_cli_remetente
        End Get
        Set(ByVal value As Integer)
            cod_cli_remetente = value
        End Set
    End Property
    Private cod_cli_destinatario As Integer
    Public Property pCod_cli_destinatario() As Integer
        Get
            Return cod_cli_destinatario
        End Get
        Set(ByVal value As Integer)
            cod_cli_destinatario = value
        End Set
    End Property

    Private age_data As Date
    Public Property pAge_data() As Date
        Get
            Return age_data
        End Get
        Set(ByVal value As Date)
            age_data = value
        End Set
    End Property
    Private age_dt_previsao As Date
    Public Property pAge_dt_previsao() As Date
        Get
            Return age_dt_previsao
        End Get
        Set(ByVal value As Date)
            age_dt_previsao = value
        End Set
    End Property
    Private age_dt_entrega As Nullable(Of Date)
    Public Property pAge_dt_entrega As Nullable(Of Date)
        Get
            Return age_dt_entrega
        End Get
        Set(ByVal value As Nullable(Of Date))
            age_dt_entrega = value
        End Set
    End Property

    Private lCargas As New List(Of Cargas_AgendamentoBLL)
    Public Property plCargas() As List(Of Cargas_AgendamentoBLL)
        Get
            Return lCargas
        End Get
        Set(ByVal value As List(Of Cargas_AgendamentoBLL))
            lCargas = value
        End Set
    End Property

    Private pes_nome_remetente As String
    Public Property pPes_nome_remetente() As String
        Get
            Return pes_nome_remetente
        End Get
        Set(ByVal value As String)
            pes_nome_remetente = value
        End Set
    End Property

    Private pes_nome_destinatario As String
    Public Property pPes_nome_destinatario() As String
        Get
            Return pes_nome_destinatario
        End Get
        Set(ByVal value As String)
            pes_nome_destinatario = value
        End Set
    End Property

    Private pes_tipo_d As Char
    Public Property pPes_tipo_d() As Char
        Get
            Return pes_tipo_d
        End Get
        Set(ByVal value As Char)
            pes_tipo_d = value
        End Set
    End Property

    Private pes_tipo_r As Char
    Public Property pPes_tipo_r() As Char
        Get
            Return pes_tipo_r
        End Get
        Set(ByVal value As Char)
            pes_tipo_r = value
        End Set
    End Property

    Private car_protocolo As String
    Public Property pCar_protocolo() As String
        Get
            Return car_protocolo
        End Get
        Set(ByVal value As String)
            car_protocolo = value
        End Set
    End Property
    Private Verifica As Boolean
    Public Property pVerifica() As Boolean
        Get
            Return Verifica
        End Get
        Set(ByVal value As Boolean)
            Verifica = value
        End Set
    End Property
    Private tipo_carga As String
    Public Property pTipo_carga() As String
        Get
            Return tipo_carga
        End Get
        Set(ByVal value As String)
            tipo_carga = value
        End Set
    End Property

#End Region
#Region "Métodos Cargas do Agendamento"
    Public Sub carregaListaCargas(ByVal age_codigo As Integer)
        Dim objCargas As New Cargas_AgendamentoBLL
        Dim tab As DataTable
        Try
            tab = objCargas.localizarCargas(age_codigo)
            lCargas.Clear()
            For Each linha As DataRow In tab.Rows
                objCargas.pAge_codigo = linha("")
                objCargas.pCar_codigo = linha("")
                lCargas.Add(objCargas)
                objCargas = New Cargas_AgendamentoBLL
            Next
        Catch ex As Exception
            Throw ex
        End Try
        objCargas = Nothing
    End Sub
    Public Sub cadastraCargas()
        Try
            If (lCargas.Count <> 0) Then
                For Each obj As Cargas_AgendamentoBLL In lCargas
                    obj.pAge_codigo = Me.age_codigo
                    obj.inserirCargas()
                Next
            Else
                Throw New Exception("Informe a(s) carga(s) do agendamento")
            End If
            
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub carrega(ByVal age_codigo As Integer)
        Dim tab As DataTable
        Try
            Dim objCargas As New Cargas_AgendamentoBLL
            tab = objCargas.localizarCargas(age_codigo)
            If (tab.Rows.Count > 0) Then
                objCargas.pCar_codigo = tab.Rows(0).Item("car_codigo").ToString()
                carregaListaCargas(age_codigo)
            End If
            objCargas = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
#End Region
#Region "Métodos"
    Public Sub inserir()
        Try
            If (cod_cli_destinatario = 0) Then
                Throw New Exception("Informe o destinátario")
            ElseIf (cod_cli_remetente = 0) Then
                Throw New Exception("Informe o remetente")
            ElseIf (age_data > age_dt_previsao) Then
                Throw New Exception("A Data de Entrada não pode ser maior que a Data prevista de entrega")
            ElseIf (lCargas.Count = 0) Then
                Throw New Exception("Insira as cargas no agendamento para continuar")
            Else
                objDAL = New AgendamentoDAL
                Me.age_codigo = objDAL.inserir(Me.age_data, Me.age_dt_previsao, Me.age_dt_entrega, Me.cod_cli_destinatario, Me.cod_cli_remetente)
                cadastraCargas()
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub
    Public Sub alterar()
        Try
            If (cod_cli_destinatario = 0) Then
                Throw New Exception("Informe o destinátario")
            ElseIf (cod_cli_remetente = 0) Then
                Throw New Exception("Informe o remetente")
            ElseIf (age_data > age_dt_previsao) Then
                Throw New Exception("A Data de Entrada não pode ser maior que a Data prevista de entrega")
            ElseIf (age_codigo = 0) Then
                Throw New Exception("Não existe agendamento para alterar")
            ElseIf (lCargas.Count = 0) Then
                Throw New Exception("Insira as cargas no agendamento para continuar")
            Else
                objDAL = New AgendamentoDAL
                objDAL.alterar(Me.age_codigo, Me.age_data, Me.age_dt_previsao, Me.age_dt_entrega, Me.cod_cli_destinatario, Me.cod_cli_remetente)
                'excluir todos
                Dim objCargas As New Cargas_AgendamentoBLL
                objCargas.excluirCargas(Me.age_codigo)
                'incluir novamente
                cadastraCargas()
                objCargas = Nothing
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub
    Public Sub excluir()
        Try
            If (age_codigo = 0) Then
                Throw New Exception("Não existe agendamento para excluir")
            Else
                objDAL = New AgendamentoDAL
                Dim objCargas As New Cargas_AgendamentoBLL
                objCargas.excluirCargas(Me.age_codigo)
                objDAL.excluir(Me.age_codigo)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub
    Function localizarProxAnteriro(ByVal nome As String, ByVal codigo As Integer) As Integer
        Dim tab As DataTable = Nothing
        Dim CodigoP As Integer = 0
        Try
            objDAL = New AgendamentoDAL
            tab = objDAL.localizarProxAnterior(nome, codigo)
            If (tab.Rows.Count > 0) Then
                CodigoP = tab.Rows(0)("age_codigo").ToString
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return CodigoP
    End Function
    Sub VerificaExclusao(ByVal codigo As Integer)
        Try
            objDAL = New AgendamentoDAL
            Me.Verifica = objDAL.VerificaExclusao(codigo)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub
    'retorna todas as linhas pesquisadas da tabela
    Function localizarPrimeiroUltimo(ByVal nome As String) As Integer
        Dim tab As DataTable = Nothing
        Dim codigo As Integer = 0
        Try
            If (nome = "ultimo") Then
                objDAL = New AgendamentoDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("age_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("age_codigo").ToString
                End If
            ElseIf (nome = "primeiro") Then
                objDAL = New AgendamentoDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("age_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("age_codigo").ToString
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return codigo
    End Function
    Public Function localizar(ByVal codigo As Integer) As DataTable
        Dim tab As DataTable
        Try
            objDAL = New AgendamentoDAL
            tab = objDAL.localizar(codigo)
            If (tab.Rows.Count > 0) Then
                Me.age_codigo = tab.Rows(0)("age_codigo").ToString()
                Me.age_data = tab.Rows(0)("age_data").ToString()
                Me.age_dt_previsao = tab.Rows(0)("age_data_previsao").ToString()
                Dim dt As Nullable(Of Date)
                If (tab.Rows(0)("age_data_entrega") Is DBNull.Value) Then
                    dt = Nothing
                Else
                    dt = tab.Rows(0)("age_data_entrega")
                End If
                Me.age_dt_entrega = dt
                tab = objDAL.localizarCargasAgendamento(codigo)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function
    Public Sub localizarClientesAgendamento(ByVal age_codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New AgendamentoDAL
            tab = objDAL.localizarClientesAgendamento(age_codigo)
            If (tab.Rows.Count = 2) Then
                For Each linha As DataRow In tab.Rows
                    If (linha("pes_tipo_dr") = "D") Then
                        Me.cod_cli_destinatario = linha("pes_codigo").ToString()
                    Else
                        Me.cod_cli_remetente = linha("pes_codigo").ToString()
                    End If
                Next
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub
    Public Function validaCarga(ByVal protocolo As String) As Boolean
        Dim tab As DataTable
        Dim ret As Boolean = False
        Try
            objDAL = New AgendamentoDAL
            tab = objDAL.validaCarga(protocolo)
            If (tab.Rows.Count > 0) Then
                For Each linha As DataRow In tab.Rows
                    If (linha("pes_tipo_dr") = "D") Then
                        Me.pes_nome_destinatario = linha("pes_nome").ToString()
                        Me.pes_tipo_d = linha("pes_tipo_dr").ToString()
                    Else
                        Me.pes_nome_remetente = linha("pes_nome").ToString()
                        Me.pes_tipo_r = linha("pes_tipo_dr").ToString
                    End If
                Next
                Me.car_protocolo = tab.Rows(0)("car_protocolo").ToString()
                Me.age_data = tab.Rows(0)("age_data").ToString()
                ret = True
            End If
        Catch ex As Exception
            Throw ex
        End Try
        Return ret
    End Function
    Public Function verificaStatus(codigo As Integer) As DataTable
        Dim tab As DataTable
        Try
            objDAL = New AgendamentoDAL
            tab = objDAL.verificaStatus(codigo)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function
    Public Function localizar(nome As String, tipo As Char)
        Dim tab As DataTable
        Try
            objDAL = New AgendamentoDAL
            tab = objDAL.localizar(nome, tipo)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function
    Public Sub verificaLotacao(age_codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New AgendamentoDAL
            tab = objDAL.verificaLotacao(age_codigo)
            If (tab.Rows.Count > 0) Then
                Throw New Exception("Esse agendamento está alocado em um caminhão, " & vbNewLine &
                                    "exclua o agendamento da alocação")
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub localizarTipoCarga(codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New AgendamentoDAL
            tab = objDAL.localizarTipoCarga(codigo)
            If (tab.Rows.Count = 1) Then
                Me.tipo_carga = tab.Rows(0)("tc_descricao").ToString()
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub
#End Region
End Class
